package com.shwread.android.qysw.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shwread.android.qysw10000152.R;
import com.shwread.android.ui.dialog.DeleteShoppingCartGoodsDialog;
import com.shwread.android.ui.dialog.DialogCommonOkListenet;
import com.shwread.http.ActionHelp;

import java.util.ArrayList;
import java.util.List;

import logic.bean.ShoppingCart;
import logic.constants.DefaultConsts;
import logic.dao.ShoppingCartDao;
import logic.util.MoneyUtil;
import logic.util.PicassoUtil;
import logic.util.Utils;


/**
 * @author xuzhijun
 * @date 2015年3月24日
 */
public class ShoppingCartActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	public static final int          SHOPPING_CART_REQUEST_CODE                  = 100;
	public static final int          SHOPPING_CART_RESULT_CODE                   = 101;
	public static final int          SHOPPING_CART_RESULT_CODE_SUBMIT_ERROR_6004 = 104;
	public static final String       SUBMIT_STATE                                = "submitState";
	public static final String       SUBMIT_ERROR_MESSAGE                        = "errorMessage";
	public static final String       SUBMIT_OK                                   = "OK";
	private Context                  context;
	private TextView                 tvTitle;
	private Button                   btnHome;
	private Button                   btnEdit;
	private Button                   btnBack;
	private ListView                 listView;
	private ListView                 listErrorView;
	//	private View                topView;
	private TextView                 topTv;
	private TextView                 topEdt;
	
	private List<ShoppingCart>       goodsList;
	private List<ShoppingCart>       goodsErrorList;
	
	private ShoppingCartAdapter      adapter;
	private ShoppingCartErrorAdapter errorAdapter;
	
	private TextView                 tvBtn;
	private TextView                 tvTotalPrice;
	private TextView                 tvDeliverMNY;
	private Button                   btnClear;
	
	private boolean                  editShow                                    = false;
	private double                   totalPrice;
	private Dialog                   dialog;
	private ShoppingCartDao          dao;
	
	private LinearLayout             llayEmpeyView;
	private RelativeLayout           rlayDefaultView;
	private TextView                 tvGotoMainPage;
	private View                     errorDividerView;
	private boolean                  editMode                                    = false;
	private List<Boolean>            errorMsgShow                                = new ArrayList<Boolean>();
	
	@Override
	public Handler getUIHandler() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopping_cart);
		context = this;
		initView();
		initData();
		setListener();
		
	}
	
	private void initView() {
		//Title
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setVisibility(View.GONE);
		btnHome = (Button) findViewById(R.id.common_header_home);
		btnHome.setVisibility(View.VISIBLE);
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		btnEdit = (Button) findViewById(R.id.shopping_cart_edit);
		
		//		topView = LayoutInflater.from(context).inflate(R.layout.common_goods_item_title, null);
		//		topTv = (TextView) topView.findViewById(R.id.common_item_title_tv);
		////		topTv.setVisibility(View.GONE);
		//		topTv.setText("购物车");
		//		topEdt = (TextView) topView.findViewById(R.id.common_item_edt_btn);
		//		//		topEdt.setVisibility(View.VISIBLE);//垃圾桶
		
		tvBtn = (TextView) findViewById(R.id.total_price_bottom_buy_tv);
		tvTotalPrice = (TextView) findViewById(R.id.total_price_bottom_total_price_tv);
		tvDeliverMNY = (TextView) findViewById(R.id.total_price_bottom_nodelivermny_tv);
		tvDeliverMNY.setVisibility(View.VISIBLE);
		btnClear = (Button) findViewById(R.id.total_price_bottom_clear_btn);
		btnClear.setVisibility(View.VISIBLE);
		
		listView = (ListView) findViewById(R.id.shopping_cart_listview);
		//		listView.addHeaderView(topView);
		listErrorView = (ListView) findViewById(R.id.shopping_cart_error_listview);
		
		llayEmpeyView = (LinearLayout) findViewById(R.id.shopping_cart_empty_llay);
		tvGotoMainPage = (TextView) findViewById(R.id.shopping_cart_goto_main_page_tv);
		rlayDefaultView = (RelativeLayout) findViewById(R.id.shopping_cart_default_rlay);
		errorDividerView = findViewById(R.id.shopping_cart_error_divider);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		goodsList = dao.queryByAccount(ActionHelp.getAccount());
		goodsErrorList = dao.queryByFail(ActionHelp.getAccount());
		errorMsgShow.clear();
		for (int i = 0; i < goodsErrorList.size(); i++)
		{
			errorMsgShow.add(true);
		}
		goodsList.remove(goodsErrorList);
		adapter = new ShoppingCartAdapter(goodsList);
		errorAdapter = new ShoppingCartErrorAdapter(goodsErrorList);
		if (goodsErrorList.size() > 0)
		{
			errorDividerView.setVisibility(View.VISIBLE);
		}
		else
		{
			errorDividerView.setVisibility(View.GONE);
		}
		listView.setAdapter(adapter);
		listErrorView.setAdapter(errorAdapter);
		adapter.notifyDataSetChanged();
		errorAdapter.notifyDataSetChanged();
		calPrice();
	}
	
	private void initData() {
		goodsList = new ArrayList<ShoppingCart>();
		goodsErrorList = new ArrayList<ShoppingCart>();
		dao = ShoppingCartDao.getInstance(context);
		
		goodsList = dao.queryByAccount(ActionHelp.getAccount());
		goodsErrorList = dao.queryByFail(ActionHelp.getAccount());
		goodsList.remove(goodsErrorList);
		
		if (goodsList.size() <= 0 && goodsErrorList.size() <= 0)
		{
			llayEmpeyView.setVisibility(View.VISIBLE);
			tvGotoMainPage.setVisibility(View.VISIBLE);
			rlayDefaultView.setVisibility(View.GONE);
		}
		else
		{
			rlayDefaultView.setVisibility(View.VISIBLE);
			llayEmpeyView.setVisibility(View.GONE);
			adapter = new ShoppingCartAdapter(goodsList);
			errorAdapter = new ShoppingCartErrorAdapter(goodsErrorList);
			calPrice();
			if (goodsErrorList.size() > 0)
			{
				errorDividerView.setVisibility(View.VISIBLE);
			}
			else
			{
				errorDividerView.setVisibility(View.GONE);
			}
			listErrorView.setAdapter(errorAdapter);
			listView.setAdapter(adapter);
		}
	}
	
	private void setListener() {
		btnHome.setOnClickListener(this);
		btnEdit.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		//		topEdt.setOnClickListener(this);//垃圾桶
		tvBtn.setOnClickListener(this);
		btnClear.setOnClickListener(this);
		tvGotoMainPage.setOnClickListener(this);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(ShoppingCartActivity.this, NewGoodsMainActivity.class);
				long goodsId = goodsList.get(position - listView.getHeaderViewsCount()).goodsId;
				String detailUrl = goodsList.get(position - listView.getHeaderViewsCount()).goodsDetailUrl;
				String baseUrl = goodsList.get(position - listView.getHeaderViewsCount()).baseUrl;
				long activityId = goodsList.get(position - listView.getHeaderViewsCount()).activityId;
				int freeMoney = goodsList.get(position - listView.getHeaderViewsCount()).freeMoney;
				
				intent.putExtra(DefaultConsts.GOODS_ID_TO_WAP, goodsId);
				intent.putExtra(DefaultConsts.GOODS_BASE_URL_TO_WAP, baseUrl);
				intent.putExtra(DefaultConsts.GOODS_DETAIL_URL_TO_WAP, detailUrl);
				intent.putExtra(DefaultConsts.GOODS_ACTIVITY_PRICE_ID, activityId);
				intent.putExtra(DefaultConsts.GOODS_FREE_MONEY_TO_WAP, freeMoney);
				startActivity(intent);
			}
		});
		listView.setOnItemLongClickListener(new OnItemLongClickListener()
		{
			
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
				dialog = new DeleteShoppingCartGoodsDialog(context, "确认删除该商品?", new DialogCommonOkListenet()
				{
					
					@Override
					public void clickOk() {
						ShoppingCart reShoppingCart = goodsList.get(position);
						dao.deleteById(ActionHelp.getAccount(), reShoppingCart.id);
						goodsList.remove(position);
						adapter.notifyDataSetChanged();
						calPrice();
						dialog.dismiss();
					}
				});
				dialog.setCanceledOnTouchOutside(false);
				dialog.show();
				return true;
			}
		});
		listErrorView.setOnItemLongClickListener(new OnItemLongClickListener()
		{
			
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
				dialog = new DeleteShoppingCartGoodsDialog(context, "确认删除该商品?", new DialogCommonOkListenet()
				{
					
					@Override
					public void clickOk() {
						ShoppingCart reShoppingCart = goodsErrorList.get(position);
						dao.deleteById(ActionHelp.getAccount(), reShoppingCart.id);
						goodsErrorList.remove(position);
						errorAdapter.notifyDataSetChanged();
						calPrice();
						dialog.dismiss();
					}
				});
				dialog.setCanceledOnTouchOutside(false);
				dialog.show();
				return true;
			}
		});
	}
	
	private void calPrice() {
		totalPrice = 0;
		setBtn();
		for (ShoppingCart goods : goodsList)
		{
			double price;
			price = ((goods.salePrice - goods.freeMoney) * goods.count);
			totalPrice += price;
		}
		tvTotalPrice.setText(" ¥" + Utils.getMoneyFormat(MoneyUtil.divide(totalPrice, 100)));
		tvBtn.setText("结算(" + goodsList.size() + ")");
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_home:
				//				Utils.ToastShow(context, "点击了返回主页");
				//				startActivity(new Intent(ShoppingCartActivity.this, MainActivity.class));
				BaseSimpleActivity.gotoMainActivity();
				break;
			case R.id.shopping_cart_edit:
				if (editMode)
				{
					editMode = false;
					btnEdit.setText("编辑");
				}
				else
				{
					editMode = true;
					btnEdit.setText("完成");
				}
				adapter.notifyDataSetChanged();
				errorAdapter.notifyDataSetChanged();
				break;
			case R.id.common_header_back:
				ShoppingCartActivity.this.finish();
				break;
			case R.id.common_item_edt_btn:
				editShow = !editShow;
				adapter.notifyDataSetChanged();
				break;
			case R.id.total_price_bottom_buy_tv:
				if (editMode)
				{
					editMode = false;
					btnEdit.setText("编辑");
					adapter.notifyDataSetChanged();
					errorAdapter.notifyDataSetChanged();
				}
				if (goodsList.size() == 0)
				{
					Utils.ToastShow(context, "购物车为空哦");
					break;
				}
				Intent confirmOrderIntent = new Intent(this, ConfirmOrderActivity.class);
				confirmOrderIntent.putExtra(DefaultConsts.FROM_TYPE, DefaultConsts.CONFIRM_ORDER_FROM_SHOPPING_CART);
				startActivityForResult(confirmOrderIntent, SHOPPING_CART_REQUEST_CODE);
				//				this.finish();
				break;
			case R.id.total_price_bottom_clear_btn:
				
				//清空
				
				dialog = new DeleteShoppingCartGoodsDialog(context, "确认清空购物车中所有商品 ？", new DialogCommonOkListenet()
				{
					
					@Override
					public void clickOk() {
						dao.deleteAll(ActionHelp.getAccount());
						if (dao.queryCount(ActionHelp.getAccount()) == 0)
						{
							if (goodsList.size() > 0) goodsList.clear();
							if (goodsErrorList.size() > 0) goodsErrorList.clear();
							adapter.notifyDataSetChanged();
						}
						
						calPrice();
					}
				});
				dialog.show();
				
				break;
			case R.id.shopping_cart_goto_main_page_tv:
				BaseSimpleActivity.gotoMainActivity();
				break;
			default:
				break;
		}
	}
	
	private void setBtn() {
		int padding = tvBtn.getPaddingLeft();
		//		if (goodsList != null && goodsList.size() > 0)
		//		{
		//			tvBtn.setBackgroundResource(R.drawable.button_make_order);
		//			tvBtn.setPadding(padding, 0, padding, 0);
		//			tvBtn.setClickable(true);
		//		}
		//		else
		//		{
		//		}
		if (goodsList == null || goodsList.size() <= 0)
		{
			btnEdit.setVisibility(View.GONE);
		}
		else
		{
			btnEdit.setVisibility(View.VISIBLE);
		}
		
		if (goodsList.size() <= 0 && goodsErrorList.size() <= 0)
		{
			btnClear.setVisibility(View.GONE);
			tvBtn.setBackgroundResource(R.drawable.buttonbg_gray);
			tvBtn.setPadding(padding, 0, padding, 0);
			
			llayEmpeyView.setVisibility(View.VISIBLE);
			tvGotoMainPage.setVisibility(View.VISIBLE);
			rlayDefaultView.setVisibility(View.GONE);
		}
		else if (goodsList.size() <= 0 && goodsErrorList.size() > 0)
		{
			tvBtn.setBackgroundResource(R.drawable.buttonbg_gray);
			tvBtn.setPadding(padding, 0, padding, 0);
			
			errorDividerView.setVisibility(View.GONE);
		}
		else if (goodsList.size() > 0 && goodsErrorList.size() <= 0)
		{
			tvBtn.setBackgroundResource(R.drawable.button_make_order);
			tvBtn.setPadding(padding, 0, padding, 0);
			tvBtn.setClickable(true);
			errorDividerView.setVisibility(View.GONE);
		}
		else
		{
			tvBtn.setBackgroundResource(R.drawable.button_make_order);
			tvBtn.setPadding(padding, 0, padding, 0);
			tvBtn.setClickable(true);
			errorDividerView.setVisibility(View.VISIBLE);
			rlayDefaultView.setVisibility(View.VISIBLE);
			llayEmpeyView.setVisibility(View.GONE);
		}
		
	}
	
	private class ShoppingCartAdapter
	        extends
	        BaseAdapter
	{
		private ViewHolder         holder;
		
		private List<ShoppingCart> datas;
		
		public ShoppingCartAdapter(List<ShoppingCart> goodsList) {
			this.datas = goodsList;
		}
		
		@Override
		public int getCount() {
			return datas.size();
		}
		
		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.shopping_cart_item, parent, false);
				holder.tvGoodsName = (TextView) convertView.findViewById(R.id.shopping_cart_item_name_tv);
				holder.rayPlusMinus = (FrameLayout) convertView.findViewById(R.id.shopping_cart_item_plus_minus_fl);
				holder.btnMinus = (Button) convertView.findViewById(R.id.shopping_cart_item_minus_btn);
				holder.tvNumber = (TextView) convertView.findViewById(R.id.shopping_cart_item_number_tv);
				holder.btnPlus = (Button) convertView.findViewById(R.id.shopping_cart_item_plus_btn);
				holder.tvClassify = (TextView) convertView.findViewById(R.id.shopping_cart_item_classify_tv);
				holder.tvPrice = (TextView) convertView.findViewById(R.id.shopping_cart_item_price_tv);
				holder.tvCount = (TextView) convertView.findViewById(R.id.shopping_cart_item_count_tv);
				holder.imgCover = (ImageView) convertView.findViewById(R.id.shopping_cart_item_image);
				holder.layPC = (LinearLayout) convertView.findViewById(R.id.shopping_cart_item_price_count_lay);
				holder.imgDelete = (ImageView) convertView.findViewById(R.id.shopping_cart_item_delete_img);
//				holder.imgCover.setImageUrl(datas.get(position).picUrl);
				PicassoUtil.load(context, datas.get(position).picUrl,R.drawable.bg_small_image,holder.imgCover);
				holder.url = datas.get(position).picUrl;
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}


			if (editMode)
			{
				holder.tvGoodsName.setVisibility(View.GONE);
				holder.rayPlusMinus.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.tvGoodsName.setVisibility(View.VISIBLE);
				holder.rayPlusMinus.setVisibility(View.GONE);
			}
			
			holder.tvGoodsName.setText(datas.get(position).title);
			holder.tvNumber.setText("" + datas.get(position).count);
			holder.tvClassify.setText(datas.get(position).skuPropDetail);
			holder.tvPrice.setText(" ¥" + Utils.getMoneyFormat(MoneyUtil.divide(datas.get(position).salePrice - datas.get(position).freeMoney, 100)));
			holder.tvCount.setText("x" + datas.get(position).count);
			//垃圾桶
			//			holder.imgDelete.setOnClickListener(new OnClickListener()
			//			{
			//				
			//				@Override
			//				public void onClick(View v) {
			//					dialog = new DeleteShoppingCartGoodsDialog(context, new DialogCommonOkListenet()
			//					{
			//						
			//						@Override
			//						public void clickOk() {
			//							datas.remove(position);
			//							adapter.notifyDataSetChanged();
			//							calPrice();
			//							dialog.dismiss();
			//						}
			//					});
			//					dialog.show();
			//				}
			//			});
			if (holder.url != datas.get(position).picUrl)
			{
//				holder.imgCover.setImageUrl(datas.get(position).picUrl);
				PicassoUtil.load(context, datas.get(position).picUrl,R.drawable.bg_small_image,holder.imgCover);
				holder.url = datas.get(position).picUrl;
			}
			//			if (editShow)
			//			{
			//				topEdt.setText(context.getResources().getString(R.string.common_goods_finish));
			//				holder.imgDelete.setVisibility(View.VISIBLE);
			//				holder.layPC.setVisibility(View.GONE);
			//			}
			//			else
			//			{
			//				topEdt.setText(context.getResources().getString(R.string.common_goods_edt));
			//				holder.imgDelete.setVisibility(View.GONE);
			//				holder.layPC.setVisibility(View.VISIBLE);
			//			}
			holder.btnMinus.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v) {
					if (datas.get(position).count > 1)
					{
						datas.get(position).count--;
						adapter.notifyDataSetChanged();
						dao.update(datas.get(position));
						calPrice();
					}
				}
			});
			holder.btnPlus.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v) {
					if (datas.get(position).count < 99)
					{
						datas.get(position).count++;
						adapter.notifyDataSetChanged();
						dao.update(datas.get(position));
						calPrice();
					}
				}
			});
			return convertView;
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == SHOPPING_CART_RESULT_CODE)
		{
			if (data != null && data.getExtras() != null && data.getExtras().getString(SUBMIT_STATE).equals(SUBMIT_OK))
			{
				this.finish();
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//	//购物车商品异常测试    
		//		for (ShoppingCart cart : goodsErrorList)
		//		{
		//			cart.ordersStatus = ShoppingCart.ORDERSTATUS_NORMAL;
		//			dao.update(cart);
		//		}
		
	}
	
	class ViewHolder
	{
		public TextView       tvGoodsName;
		public TextView       tvClassify;
		public TextView       tvPrice;
		public TextView       tvCount;
		public ImageView imgCover;
		public ImageView      imgDelete;
		public LinearLayout   layPC;
		public String         url;
		public TextView       tvErrorMessage;
		public FrameLayout    rayError;
		public FrameLayout    rayPlusMinus;
		public Button         btnMinus;
		public TextView       tvNumber;
		public Button         btnPlus;
	}
	
	private class ShoppingCartErrorAdapter
	        extends
	        BaseAdapter
	{
		private ViewHolder         holder;
		
		private List<ShoppingCart> datas;
		
		public ShoppingCartErrorAdapter(List<ShoppingCart> errorGoodsList) {
			this.datas = errorGoodsList;
		}
		
		@Override
		public int getCount() {
			return datas.size();
		}
		
		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@SuppressLint ("NewApi")
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.shopping_cart_item, parent, false);
				holder.tvGoodsName = (TextView) convertView.findViewById(R.id.shopping_cart_item_name_tv);
				holder.rayPlusMinus = (FrameLayout) convertView.findViewById(R.id.shopping_cart_item_plus_minus_fl);
				holder.btnMinus = (Button) convertView.findViewById(R.id.shopping_cart_item_minus_btn);
				holder.tvNumber = (TextView) convertView.findViewById(R.id.shopping_cart_item_number_tv);
				holder.btnPlus = (Button) convertView.findViewById(R.id.shopping_cart_item_plus_btn);
				holder.tvClassify = (TextView) convertView.findViewById(R.id.shopping_cart_item_classify_tv);
				holder.tvPrice = (TextView) convertView.findViewById(R.id.shopping_cart_item_price_tv);
				holder.tvCount = (TextView) convertView.findViewById(R.id.shopping_cart_item_count_tv);
				holder.imgCover = (ImageView) convertView.findViewById(R.id.shopping_cart_item_image);
				holder.layPC = (LinearLayout) convertView.findViewById(R.id.shopping_cart_item_price_count_lay);
				holder.imgDelete = (ImageView) convertView.findViewById(R.id.shopping_cart_item_delete_img);
				holder.rayError = (FrameLayout) convertView.findViewById(R.id.shopping_cart_item_error_fay);
				holder.rayError.setVisibility(View.VISIBLE);
//				holder.imgCover.setImageUrl(datas.get(position).picUrl);
//				PicassoUtil.load(context, datas.get(position).picUrl,R.drawable.bg_small_image,holder.imgCover);
				holder.url = datas.get(position).picUrl;
				holder.tvErrorMessage = (TextView) convertView.findViewById(R.id.shopping_cart_item_error_tv);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			
			if (editMode && datas.get(position).errorMsg.equals("库存不足"))
			{
				holder.tvGoodsName.setVisibility(View.GONE);
				holder.rayPlusMinus.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.tvGoodsName.setVisibility(View.VISIBLE);
				holder.rayPlusMinus.setVisibility(View.GONE);
			}
			
			holder.tvGoodsName.setText(datas.get(position).title);
			holder.tvNumber.setText("" + datas.get(position).count);
			holder.tvClassify.setText(datas.get(position).skuPropDetail);
			holder.tvPrice.setText(" ¥" + Utils.getMoneyFormat(MoneyUtil.divide(datas.get(position).salePrice - datas.get(position).freeMoney, 100)));
			holder.tvCount.setText("x" + datas.get(position).count);
			holder.tvErrorMessage.setText(datas.get(position).errorMsg);
			if (errorMsgShow.get(position))
			{
				holder.tvErrorMessage.setVisibility(View.VISIBLE);
				holder.rayError.setVisibility(View.VISIBLE);
			}
			else
			{
				holder.tvErrorMessage.setVisibility(View.GONE);
				holder.rayError.setVisibility(View.GONE);
			}
			if (holder.url != datas.get(position).picUrl)
			{
//				holder.imgCover.setImageUrl(datas.get(position).picUrl);
				PicassoUtil.load(context, datas.get(position).picUrl,R.drawable.bg_small_image,holder.imgCover);
				holder.url = datas.get(position).picUrl;
			}
			
			holder.btnMinus.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v) {
					if (datas.get(position).count > 1)
					{
						datas.get(position).ordersStatus = ShoppingCart.ORDERSTATUS_NORMAL;
						datas.get(position).errorCode = "";
						datas.get(position).errorMsg = "";
						dao.update(datas.get(position));
						onResume();
					}
				}
			});
			holder.btnPlus.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v) {
					if (datas.get(position).count < 99)
					{
						datas.get(position).ordersStatus = ShoppingCart.ORDERSTATUS_NORMAL;
						datas.get(position).errorCode = "";
						datas.get(position).errorMsg = "";
						dao.update(datas.get(position));
						onResume();
					}
				}
			});
			return convertView;
		}
		
	}
	
}
